<?php

/**
 * @author thieule
 * @copyright 2011
 */

include('config.php');
function curPageURL() {
 $pageURL = 'http';
 if (@$_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$urlcurrent=str_replace('exportcsv.php','',curPageURL());

$ls=$db->loadObjectList('select * from users');
   
    $csv = "Họ và tên\tNgày sinh\tQuốc tịch\tTình trạng hôn nhân\tNghề nghiệp\tEmail\tDi động\tHình 1\tHình 2\tHình 3\tHình 4\tHình5\tVideo\tMô tả\tNgày đăng ký\n";
    if(count($ls))
    {
        foreach($ls as $row)
        {
            $r=array();
            $r[]=$row->name;
            $r[]=date('d/m/Y',strtotime($row->birthday));
            $r[]=empty($row->country)?'Ngoài Việt Nam':'Việt Nam';
            $r[]=empty($row->family)?'Độc thân':'Đã có gia đình';
            $r[]=$row->job;
            $r[]=$row->email;
            $r[]="'".$row->phone."";
            $r[]=empty($row->images1)?'':$urlcurrent.$conf['folder'].$row->images1;
            $r[]=empty($row->images2)?'':$urlcurrent.$conf['folder'].$row->images2;
            $r[]=empty($row->images3)?'':$urlcurrent.$conf['folder'].$row->images3;
            $r[]=empty($row->images4)?'':$urlcurrent.$conf['folder'].$row->images4;
            $r[]=empty($row->images5)?'':$urlcurrent.$conf['folder'].$row->images5;
            $r[]=empty($row->video)?'':$urlcurrent.$conf['folder'].$row->video;
            $r[]=str_replace(array("\n","\r"),array("",""),$row->desc);
            $r[]=date('d/m/Y',$row->created_date);
            
            $csv .= trim(join("\t", $r))."\n";
        }
    }
    $csv = chr(255).chr(254).mb_convert_encoding($csv, "UTF-16LE", "UTF-8");
    header("Content-type: application/x-msdownload");
    header("Content-disposition: csv; filename=report_" . date("Y_m_d_H_i_s").".csv; size=".strlen($csv));
    print $csv;
    exit();

?>